package net.caixiaomi.info.widgets;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebView;

public class WebView4Scroll extends WebView {
    private ViewGroup mParent;

    public WebView4Scroll(Context context) {
        super(context);
    }

    public WebView4Scroll(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setViewGroup(ViewGroup parent) {
        mParent = parent;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                    this.evaluateJavascript("$(\"#content\").scrollTop()", new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String value) {
                            if(!TextUtils.isEmpty(value) && !TextUtils.equals("null",value)){
                                if(Integer.parseInt(value) == 0){
                                    mParent.setEnabled(true);
                                }else{
                                    mParent.setEnabled(false);
                                }
                            }
                        }
                    });
                }
            }
            default:
                break;

        }
        return super.onTouchEvent(event);
    }
}
